package com.cloudm.technician.di

import android.content.Context
import com.cloudm.technician.data.config.Config
import com.cloudm.technician.data.db.AppDbHelper
import com.cloudm.technician.data.db.DbHelper
import com.cloudm.technician.data.preferences.AppPreferencesHelper
import com.cloudm.technician.data.preferences.PreferencesHelper
import dagger.Module
import dagger.Provides
import io.realm.Realm
import io.realm.RealmConfiguration
import javax.inject.Singleton

/**
 * 作者: wesley
 * 时间: 2017/11/20
 * 描述: 提供preferences和db的实例
 */

@Module
class DbPreferencesModule(private val context: Context) {

    @Singleton
    @Provides
    fun provideDbHelper(): DbHelper {
        val realm = Realm.getInstance(RealmConfiguration.Builder().name(
                Config.REALM_NAME).deleteRealmIfMigrationNeeded().build())
        return AppDbHelper(realm)
    }

    @Singleton
    @Provides
    fun providePreferencesHelper(): PreferencesHelper = AppPreferencesHelper(
            context.getSharedPreferences(Config.PREFERENCES_NAME, Context.MODE_PRIVATE))
}